<?php
namespace Admin\Controller;

class KebiaoController extends AdminController {
    public function index(){
    	//定义搜索项
    	$this->assign('schoolList', R('Common/getSchoolList'));
    	$this->assign('xiList', R('Common/getXiList'));
    	$this->assign('nianjiList', R('Common/getNianjiList'));
    	$this->assign('banjiList', R('Common/getBanjiList'));
    	$this->assign('weekList', C('WEEKS'));
    	$searchItems = array(
    		array('select', 'school_id', '所属学校', 'schoolList'),
    		array('select', 'xi_id', '所属系', 'xiList'),
    		array('select', 'nianji_id', '所属年级', 'nianjiList'),
    		array('select', 'banji_id', '所属班级', 'banjiList'),
    		array('select', 'week_id', '星期', 'weekList'),
    	);
    	$this->assign('searchItems', $searchItems);
    	
    	$map = array();
    	$searchFields = array('school_id', 'xi_id', 'nianji_id', 'banji_id', 'week_id');
    	foreach ($searchFields as $field){
    		if (I($field) > 0) {
    			$map[$field] = I($field);
    		}
    	}
    	$list   = $this->lists('Kebiao', $map);
    	
        $this->assign('_list', $list);
        $this->meta_title = '课表列表';
        $this->display();
    }
    
    Public function add(){
    	$this->assign('schoolList', R('Common/getSchoolList'));
    	$this->assign('xiList', R('Common/getXiList'));
    	$this->assign('nianjiList', R('Common/getNianjiList'));
    	$this->assign('banjiList', R('Common/getBanjiList'));
    	$this->assign('weekList', C('WEEKS'));
    	$this->assign('timeList', C('KEBIAO_TIME_LIST'));
    	$this->assign('timeListJson', json_encode(C('KEBIAO_TIME_LIST')));
    	$this->meta_title = '添加课表';
    	$this->display();
    }
    
    Public function edit(){
    	$id = I('get.id');
    	$info = D('Kebiao')->where(array('id' => $id))->find();
    	$content = unserialize($info['content']);
    	$this->assign('info', $info);
    	$this->assign('content', $content);
    	$this->assign('schoolList', R('Common/getSchoolList'));
    	$this->assign('xiList', R('Common/getXiList'));
    	$this->assign('nianjiList', R('Common/getNianjiList'));
    	$this->assign('banjiList', R('Common/getBanjiList'));
    	$this->assign('weekList', C('WEEKS'));
    	$this->assign('timeList', C('KEBIAO_TIME_LIST'));
    	$this->assign('timeListJson', json_encode(C('KEBIAO_TIME_LIST')));
    	$this->meta_title = '修改课表';
    	$this->display();
    }
    
    public function genTimeList(){
    	$index = 1;
    	/* for ($hour = 6; $hour < 21; $hour ++){
    		for ($min = 0; $min < 60; $min += 15){
    			if ($min == 0) {
    				$min = '00';
    			}
    			$str = $hour . ' : ' . $min;
    			echo $index . ' => \'' . $str . '\',<br/>';
    			$index ++;
    		}
    	} */
    	for ($hour = 6; $hour < 21; $hour ++){
    		for ($min = 0; $min < 60; $min += 15){
    			if ($min == 0) {
    				$min = '00';
    			}
    			$str = $hour . ' : ' . $min;
    			echo "'" . $str . "',<br/> ";
    			$index ++;
    		}
    	}
    }
    
    public function doUpdate(){
    	if (IS_POST) {
    		$schoolId 	= I('post.school_id');
    		$xiId 		= I('post.xi_id');
    		$nianjiId 	= I('post.nianji_id');
    		$banjiId 	= I('post.banji_id');
    		$weekId 	= I('post.week_id');
    		if ($schoolId * $xiId * $nianjiId * $banjiId * $weekId == 0) {
    			$this->error('学校，系，年级，班级，星期必选!');
    		}
    		$startTimeArr 	= I('post.start_time_id');
    		$endTimeArr 	= I('post.end_time_id');
    		$titleArr		= I('post.title');
    		
    		/**
    		 * 清洗数据
    		 * 前端JS已经处理，一般不会出现问题
    		 */
    		
    		//组装数据
    		$kebiaoArr = array();
    		foreach ($startTimeArr as $key => $val){
    			if ($val > 0 && $endTimeArr[$key] > 0 && !empty($titleArr[$key])) {
    				$kebiaoArr[] = array('start_time_id' => $val, 'end_time_id' => $endTimeArr[$key], 'title' => $titleArr[$key]);
    			}
    		}
    		if (empty($kebiaoArr)) {
    			$this->error('请录入课程信息!');
    		}
    		
    		//入库
    		$data = array();
    		$data['school_id'] 	 = $schoolId;
    		$data['xi_id'] 		 = $xiId;
    		$data['nianji_id'] 	 = $nianjiId;
    		$data['banji_id'] 	 = $banjiId;
    		$data['week_id'] 	 = $weekId;
    		$data['content']	 = serialize($kebiaoArr);
    		$data['create_time'] = time();
    		
    		//若已存在，先删除
    		D('Kebiao')->where(array('school_id' => $schoolId, 'xi_id' => $xiId, 'nianji_id' => $nianjiId, 'banji_id' => $banjiId, 'week_id' => $weekId))->delete();
    		$ret = D('Kebiao')->add($data);
    		if ($ret !== false){
    			$this->success('操作成功', U('index'));
    		}
    	}
    }
    
    public function del(){
    	$id = I('get.id');
    	$id = intval($id);
    	$ret = D('Kebiao')->where(array('id' => $id))->delete();
    	if ($ret !== false) {
    		$this->success('删除成功');
    	}
    }
}